package gervosou;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JSeparator;

public class VueConsultComptePerso extends Vue {

	private ComptePerso _compte ;
	
	private JPanel contentPane;
	private JTextField textFieldNum;
	
	private JButton btnConsulter;
	private JButton btnAnnuler;
	private JButton btnFermer;
	private JButton btnNouvelleRecherche;
	
	private JTextField textFieldNom;
	private JTextField textFieldPrenom;
	private JTextField textFieldAdresse;
	private JTextField textFieldDateNaissance;
	private JTextField textFieldNumCompte;
	private JTextField textFieldSolde;
	private JTextField textFieldDecouvert;
	
	/**
	 * Create the frame.
	 */
	public VueConsultComptePerso(Controleur controleur) {
		super(controleur);
		setTitle("Consulter un compte personnel");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 484, 589);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNom = new JLabel("Numéro de comptre : ");
		lblNom.setBounds(28, 53, 148, 16);
		contentPane.add(lblNom);
		
		textFieldNum = new JTextField();
		textFieldNum.setBounds(173, 48, 134, 28);
		contentPane.add(textFieldNum);
		textFieldNum.setColumns(10);
		
		
		btnConsulter = new JButton("Consulter");
		btnConsulter.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int num = Integer.parseInt(textFieldNum.getText());
				// liaison de la vue avec l'objet observe
				setComptePerso (getControleur().rechComptePerso(num));
			}
			});
		
		btnConsulter.setBounds(277, 93, 117, 29);
		contentPane.add(btnConsulter);
		
		btnAnnuler = new JButton("Annuler");
		btnAnnuler.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					getControleur().fermerVue(VueConsultComptePerso.this);}
		});
		btnAnnuler.setBounds(80, 93, 117, 29);
		contentPane.add(btnAnnuler);
		
		JSeparator separator = new JSeparator();
		separator.setBounds(226, 145, -50, 2);
		contentPane.add(separator);
		
		JSeparator separator_1 = new JSeparator();
		separator_1.setBounds(89, 145, 328, -11);
		contentPane.add(separator_1);
		
		JSeparator separator_2 = new JSeparator();
		separator_2.setBounds(60, 134, 367, 16);
		contentPane.add(separator_2);
		
		JLabel lblNomClient = new JLabel("Nom : ");
		lblNomClient.setBounds(81, 175, 98, 15);
		contentPane.add(lblNomClient);
		
		JLabel lblPrenomClient_1 = new JLabel("Prenom : ");
		lblPrenomClient_1.setBounds(81, 209, 98, 15);
		contentPane.add(lblPrenomClient_1);
		
		JLabel lblPrenomClient = new JLabel("Adresse : ");
		lblPrenomClient.setBounds(78, 247, 98, 15);
		contentPane.add(lblPrenomClient);
		
		JLabel lblDateDeNaissance = new JLabel("Date de naissance : ");
		lblDateDeNaissance.setBounds(78, 283, 139, 15);
		contentPane.add(lblDateDeNaissance);
		
		JLabel lblNumro = new JLabel("Numéro : ");
		lblNumro.setBounds(78, 374, 98, 15);
		contentPane.add(lblNumro);
		
		JLabel lblSolde = new JLabel("Solde : ");
		lblSolde.setBounds(78, 408, 98, 15);
		contentPane.add(lblSolde);
		
		JLabel lblDecouvertAutoris = new JLabel("Decouvert autorisé : ");
		lblDecouvertAutoris.setBounds(78, 445, 139, 15);
		contentPane.add(lblDecouvertAutoris);
		
		textFieldNom = new JTextField();
		textFieldNom.setColumns(10);
		textFieldNom.setBounds(215, 162, 134, 28);
		contentPane.add(textFieldNom);
		
		textFieldPrenom = new JTextField();
		textFieldPrenom.setColumns(10);
		textFieldPrenom.setBounds(215, 203, 134, 28);
		contentPane.add(textFieldPrenom);
		
		textFieldAdresse = new JTextField();
		textFieldAdresse.setColumns(10);
		textFieldAdresse.setBounds(215, 241, 134, 28);
		contentPane.add(textFieldAdresse);
		
		textFieldDateNaissance = new JTextField();
		textFieldDateNaissance.setColumns(10);
		textFieldDateNaissance.setBounds(215, 281, 134, 28);
		contentPane.add(textFieldDateNaissance);
		
		textFieldNumCompte = new JTextField();
		textFieldNumCompte.setColumns(10);
		textFieldNumCompte.setBounds(215, 372, 134, 28);
		contentPane.add(textFieldNumCompte);
		
		textFieldSolde = new JTextField();
		textFieldSolde.setColumns(10);
		textFieldSolde.setBounds(215, 406, 134, 28);
		contentPane.add(textFieldSolde);
		
		textFieldDecouvert = new JTextField();
		textFieldDecouvert.setColumns(10);
		textFieldDecouvert.setBounds(215, 443, 134, 28);
		contentPane.add(textFieldDecouvert);
		
		JLabel lblInformationsClient = new JLabel("Informations client :");
		lblInformationsClient.setBounds(28, 148, 148, 15);
		contentPane.add(lblInformationsClient);
		
		JLabel lblInformationsCompte = new JLabel("Informations compte :");
		lblInformationsCompte.setBounds(28, 347, 148, 15);
		contentPane.add(lblInformationsCompte);
		
		JSeparator separator_3 = new JSeparator();
		separator_3.setBounds(50, 319, 367, 16);
		contentPane.add(separator_3);
		
		btnNouvelleRecherche = new JButton("Nouvelle recherche");
		btnNouvelleRecherche.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getControleur().fermerVue(VueConsultComptePerso.this);
				getControleur().ConsultComptePerso();
				};
			});
		btnNouvelleRecherche.setBounds(50, 498, 162, 42);
		contentPane.add(btnNouvelleRecherche);
		
		
		btnFermer = new JButton("Fermer");
		btnFermer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getControleur().fermerVue(VueConsultComptePerso.this);
				}
		});		
		btnFermer.setBounds(262, 498, 162, 42);
		contentPane.add(btnFermer);

	}

	/**
	 * definition des etats de la fenetre
	 */
	public void setEtat (int etat){
		switch (etat) {
		case initiale: {
			textFieldNum.setEditable(true);
			textFieldNom.setEditable(false);
			textFieldPrenom.setEditable(false);
			textFieldAdresse.setEditable(false);
			textFieldDateNaissance.setEditable(false);
			textFieldNumCompte.setEditable(false);
			textFieldSolde.setEditable(false);
			textFieldDecouvert.setEditable(false);

			btnConsulter.setEnabled(true);
			btnAnnuler.setEnabled(true);

			btnConsulter.setVisible(true);
			btnAnnuler.setVisible(true);
			btnFermer.setEnabled(false);
			btnNouvelleRecherche.setEnabled(false);

			break;
			}
		case finale: {
			textFieldNum.setEditable(false);
			textFieldNom.setEditable(false);
			textFieldPrenom.setEditable(false);
			textFieldAdresse.setEditable(false);
			textFieldDateNaissance.setEditable(false);
			textFieldNumCompte.setEditable(false);
			textFieldSolde.setEditable(false);
			textFieldDecouvert.setEditable(false);
			btnConsulter.setEnabled(false);
			btnAnnuler.setEnabled(false);
			btnFermer.setEnabled(true);
			btnNouvelleRecherche.setEnabled(true);
			break;
			}
		}
	}
	

	private ComptePerso getComptePerso() {
		return _compte;
	}
	
	private void setComptePerso(ComptePerso compte) {
		 _compte = compte;
	}
	


	public void alimente(ComptePerso cp) {
		
		ClientPhysique clp = cp.getClientPhysique();
		DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
		java.util.Date dateDate = clp.getDateNaissance().getTime();

		textFieldNom.setText((clp.getNom()));
		textFieldPrenom.setText(clp.getPrenom());
		textFieldAdresse.setText(clp.getAdresse());
		textFieldDateNaissance.setText(String.valueOf(dateFormat.format(dateDate)));
	
		
	    textFieldNumCompte.setText(String.valueOf(cp.getNumero()));
		textFieldSolde.setText(String.valueOf(cp.getSolde()));
		textFieldDecouvert.setText(String.valueOf(cp.getDecouvertAutorise()));
	}
	

}